#include "App.h"
#include "VertexTypes.h"


namespace{

    glm::vec4 m_ClearColor = glm::vec4(1.0f, 0.5f, 1.f, 1.0f);
    Gut::RenderTarget m_RenderTarget;
};

App::App()
    : BaseApp()
{
}

bool App::Create(const GLFWConfig &config)
{
    if (!BaseApp::Create(config))
    {
        return false;
    }

    LOGI("on create the app");
    Gut::RenderTarget::Desc desc;
    desc.width = state.mClientWidth;
    desc.height = state.mClientHeight;
    desc.numTexture = 1;

    m_RenderTarget.Create(desc);
    return true;
};

void App::OnFrameRender()
{
    glViewport(0, 0, this->state.mClientWidth, state.mClientHeight);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    m_RenderTarget.Begin();
    glClearColor(m_ClearColor.x, m_ClearColor.y, m_ClearColor.z, m_ClearColor.w);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_RenderTarget.End();
}

void App::OnFrameBufferSize(int width, int height)
{
    BaseApp::OnFrameBufferSize(width, height);
    m_RenderTarget.SetWindowSize(width, height);
}

void App::OnTick()
{
    BaseApp::OnTick();
}

void App::OnDestroy()
{
    BaseApp::OnDestroy();
};

void App::OnEdit()
{
    ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());

    if(ImGui::Begin("Hello "))
    {
        ImGui::ColorEdit4("clearColor", &m_ClearColor.x);
    }

    ImGui::End();

    if(ImGui::Begin("ImGui"))
    {
        m_GutEditor.AddFramebuffer((ImTextureID)m_RenderTarget.outColors[0].ID);
    }

    ImGui::End();

}


App::~App()
{
    LOGI("app is deconstruted");
};